package com.example.thirdhomework

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import android.content.Intent
import android.widget.Button
import android.widget.EditText
import android.widget.TextView

const val FIRST_MSG = "FIRST_MSG"
const val SECOND_MSG = "SECOND_MSG"

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)

        // 获取按钮引用
        val first: Button = findViewById(R.id.first)
        val second: Button = findViewById(R.id.second)
        val third: Button = findViewById(R.id.third)
        val fourth: Button = findViewById(R.id.fourth) // 新增的按钮引用

        // 获取EditText引用
        val editText: EditText = findViewById(R.id.editTextText)
        val textView: TextView = findViewById(R.id.textView)

        // 设置点击监听器
        first.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            intent.putExtra(FIRST_MSG, editText.text.toString())
            startActivity(intent)
        }

        second.setOnClickListener {
            val intent = Intent(this, ThirdActivity::class.java)
            startActivityForResult(intent, 0)
        }

        third.setOnClickListener {
            val intent = Intent(this, ForthActivity::class.java)
            startActivity(intent)
        }

        fourth.setOnClickListener {
            val intent = Intent(this, FifthActivity::class.java)
            startActivity(intent)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 0 && resultCode == 1) {
            val textView: TextView = findViewById(R.id.textView)
            textView.text = data?.getStringExtra(SECOND_MSG)
        }
    }
}